package com.quectel.taskserver.job.partybuilding;

import cn.hutool.core.lang.func.LambdaUtil;
import com.quectel.business.common.partybuilding.PartyPersonMsgBusiness;
import com.quectel.constant.PushConstants;
import com.quectel.constant.core.mobileuser.MobileUserMsgConstants;
import com.quectel.constant.core.partybuilding.PartyBuildingConstants;
import com.quectel.constant.core.partybuilding.PartySurveyConstants;
import com.quectel.constant.global.SystemConstants;
import com.quectel.core.module.mobileuser.dto.MobileUserDto;
import com.quectel.core.module.mobileuser.service.MobileUserService;
import com.quectel.core.module.partybuilding.dto.PartyPersonDto;
import com.quectel.core.module.partybuilding.service.PartyPersonService;
import com.quectel.core.module.partybuildingsurvey.dto.PartySurveyDto;
import com.quectel.core.module.partybuildingsurvey.dto.PartySurveyPersonBindingDto;
import com.quectel.core.module.partybuildingsurvey.service.PartySurveyPersonBindingService;
import com.quectel.core.module.partybuildingsurvey.service.PartySurveyService;
import com.quectel.util.common.DateUtils;
import com.quectel.util.kit.PageFetchKit;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.shardingsphere.elasticjob.api.ShardingContext;
import org.apache.shardingsphere.elasticjob.simple.job.SimpleJob;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.*;


/**
 * 调查问卷提醒
 *
 * @author: bob
 * @email: bob.yu@quectel.com
 * @date: 2023-06-12 8:59:00
 */
@Component
public class PartySurveyRemindTask implements SimpleJob {

    private static final Logger LOGGER = LoggerFactory.getLogger(PartySurveyRemindTask.class);

    @DubboReference
    private PartySurveyService partySurveyService;
    @DubboReference
    private PartySurveyPersonBindingService partySurveyPersonBindingService;
    @DubboReference
    private MobileUserService mobileUserService;
    @Autowired
    private PartyPersonService partyPersonService;
    @Autowired
    private PartyPersonMsgBusiness partyPersonMsgBusiness;

    @Override
    public void execute(ShardingContext shardingContext) {
        LOGGER.info("调查问卷通知，开始执行... 时间 {}", DateUtils.getCurrentDateStr(DateUtils.FormatType.COMMON));

        //查询当前时间大于提醒时间并且未提醒的
        Map<String, Object> params = new HashMap<>(4);
        params.put(LambdaUtil.getFieldName(PartySurveyDto::getNotifyStatus), SystemConstants.NO);
        params.put(LambdaUtil.getFieldName(PartySurveyDto::getRemindTime), DateUtils.format(new Date(), DateUtils.FormatType.COMMON));
        params.put(LambdaUtil.getFieldName(PartySurveyDto::getStatus), PartySurveyConstants.MarkStatus.PUBLISHED.getStatus());

        List<PartySurveyDto> partySurveyDtos = PageFetchKit.fetchAll(r -> partySurveyService.queryList(r), params);
        if (CollectionUtils.isEmpty(partySurveyDtos)) {
            LOGGER.info("调查问卷通知，结束执行... 时间 {}", DateUtils.getCurrentDateStr(DateUtils.FormatType.COMMON));
            return;
        }

        for (PartySurveyDto partySurveyDto : partySurveyDtos) {
            List<PartySurveyPersonBindingDto> partySurveyPersonBindingDtos = partySurveyPersonBindingService.selectBySurveyId(partySurveyDto.getId());
            if (CollectionUtils.isEmpty(partySurveyPersonBindingDtos)) {
                continue;
            }

            for (PartySurveyPersonBindingDto partySurveyPersonBindingDto : partySurveyPersonBindingDtos) {
                PartyPersonDto partyPersonDto = partyPersonService.selectCacheById(partySurveyPersonBindingDto.getPersonId());
                if (partyPersonDto == null) {
                    continue;
                }

                MobileUserDto mobileUserDto = mobileUserService.selectByMobile(partyPersonDto.getMobile());
                if (mobileUserDto == null) {
                    continue;
                }

                partyPersonMsgBusiness.pushMsg(
                        partySurveyDto.getTenantId(),
                        Collections.singletonList(mobileUserDto),
                        PartySurveyConstants.Category.DEMOCRATIC_VOTING.getCategory().equals(partySurveyDto.getCategory())
                                ? MobileUserMsgConstants.SourceType.PARTY_SURVEY_PUBLIC_OPINION_SURVEY_MSG : MobileUserMsgConstants.SourceType.PARTY_SURVEY_DEMOCRATIC_VOTING_MSG,
                        partySurveyDto.getId(),
                        PartySurveyConstants.Category.DEMOCRATIC_VOTING.getCategory().equals(partySurveyDto.getCategory())
                                ? PartyBuildingConstants.PARTY_SURVEY_PUBLIC_OPINION_SURVEY_REMIND_MSG_TITLE : PartyBuildingConstants.PARTY_SURVEY_DEMOCRATIC_VOTING_REMIND_MSG_TITLE,
                        partySurveyDto.getName(),
                        true,
                        new PushConstants.PushType[]{PushConstants.PushType.WECHAT_OFFICIAL}
                );

            }

            partySurveyDto.setNotifyStatus(SystemConstants.YES);
            partySurveyService.updateById(partySurveyDto);

        }
        LOGGER.info("调查问卷通知，结束执行... 时间 {}", DateUtils.getCurrentDateStr(DateUtils.FormatType.COMMON));

    }


}
